package com.example.demo.controller;

import com.example.demo.common.domain.*;
import com.example.demo.common.util.Result;
import com.example.demo.service.ComLeaveService;
import com.example.demo.service.LeaveService;
import com.example.demo.service.ManagerService;
import com.example.demo.service.OutService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/divid")
public class DividController {
    @Autowired
    LeaveService leaveService;

    @Autowired
    OutService outService;

    @Autowired
    ComLeaveService comLeaveService;

    @Autowired
    @Qualifier("divisionService")
    private ManagerService divisionService;

    public boolean check(HttpSession session){
        DivisionManager divisionManger = (DivisionManager)session.getAttribute("divisionManger");
        if(divisionManger != null){
            return true;
        }
        return false;
    }

    @RequestMapping("/agreeLeaveItem")
    @ResponseBody//根据传入的请假号，更新该请假表单中的字段 此处为部门经理ID和部门经理同意
    public Map agreeLeaveItem(Integer lid){
        divisionService.agreeLeaveItem(lid);
        return Result.success(true);
    }

    @RequestMapping("/agreeOutItem")
    @ResponseBody//根据传入的外出号，更新外出表单中的字段 此处为部门经理和部门经理同意
    public Map agreeOutItem(HttpSession session, Integer oid){
        DivisionManager divisionManger = (DivisionManager)session.getAttribute("divisionManager");
        outService.updateDivIdByPrimaryId(oid,divisionManger.getDivid());
        return Result.success(true);
    }

    @RequestMapping("/agreeComLeaveItem")
    @ResponseBody//根据传入的调休号，更新外出表单中的字段 此处为部门经理和部门经理同意
    public Map agreeComLeaveItem(HttpSession session, Integer cid){
        DivisionManager divisionManger = (DivisionManager)session.getAttribute("divisionManager");
        comLeaveService.updateDivIdByPrimaryId(cid,divisionManger.getDivid());
        return Result.success(true);
    }


    @RequestMapping("/agreeleave")//搜索所有请假申请表单，跳转到部门经理同意请假申请页面
    public String agreeleave(HttpSession session, Model model, @RequestParam(required = false,defaultValue = "1") Integer pageNo,
                             @RequestParam(required = false,defaultValue = "6") Integer pageSize) throws Exception {
        if(!check(session)){
            DivisionManager divisionManager = (DivisionManager) divisionService.getMangerInstance();
            session.setAttribute("divisionManager",divisionManager);
        }


        List<LeaveRecordPlus> leave_records = leaveService.selectAllPlus();//连接查询 请假表和员工信息表一起查询
        model.addAttribute("list",leave_records);
        return "manger/divid/leavelist";
    }
    @RequestMapping("/agreecomleave")//搜索所有外出申请表单，跳转到部门经理同意外出申请页面
    public String agreecomleave(HttpSession session, Model model, @RequestParam(required = false,defaultValue = "1") Integer pageNo,
                                @RequestParam(required = false,defaultValue = "6") Integer pageSize){
        if(!check(session)){
            DivisionManager divisionManager = new DivisionManager();
            divisionManager.setDivid(3);
            divisionManager.setDivname("部门张经理");
            session.setAttribute("divisionManager",divisionManager);
        }

        List<CompensatoryLeavePlus> compensatoryLeavePluses = comLeaveService.selectAllPlus();


        model.addAttribute("list",compensatoryLeavePluses);
        return "manger/divid/comleavelist";
    }
    @RequestMapping("/agreeout")//搜索所有外出申请表单，跳转到部门经理同意外出申请页面
    public String agreeout(HttpSession session, Model model, @RequestParam(required = false,defaultValue = "1") Integer pageNo,
                           @RequestParam(required = false,defaultValue = "6") Integer pageSize){
        if(!check(session)){
            DivisionManager divisionManager = new DivisionManager();
            divisionManager.setDivid(3);
            divisionManager.setDivname("部门张经理");
            session.setAttribute("divisionManager",divisionManager);
        }


        List<OutRecordPlus> outRecordPluses = outService.selectAllPlus();//连接查询 请假表和员工信息表一起查询
        model.addAttribute("list",outRecordPluses);
        return "manger/divid/outlist";
    }
}
